//
//  HeadImageSelectedViewController.m
//  1gene
//
//  Created by 1jiyin on 16/3/18.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
//****************头像选择****************
#import "HeadImageSelectedViewController.h"
#import "AppDelegate.h"
@interface HeadImageSelectedViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
/*
 *相册选择图片
 */
@property(nonatomic,copy)UIImageView *selectedImageView;
/*
 *本地图片
 */
@property(nonatomic,copy)NSArray *imageArray;
/*
 *是否进入相册选择
 */
@property(nonatomic,assign)NSInteger photoSelectedState;
@end

@implementation HeadImageSelectedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:[UIView new]];
    _imageArray=[NSArray array];
    [self initData];
    [self createNavigation];
    [self createUI];
    // Do any additional setup after loading the view.
}
-(void)initData
{
    _imageArray=@[@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",@"1.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg"];
}
-(void)createNavigation
{
    self.navigationController.navigationBar.barTintColor=[UIColor colorWithRed:84/255.0 green:199/255.0 blue:222/255.0 alpha:1];
    //返回
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, 25, 25);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
    title.text=@"修改头像";
    title.font=[UIFont systemFontOfSize:22];
    title.textAlignment=NSTextAlignmentCenter;
    title.textColor=[UIColor whiteColor];
    self.navigationItem.titleView=title;
    
    //完成按钮
    UIButton *rightBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame=CGRectMake(0, 0, 44, 44);
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
//完成
-(void)rightBtnClick:(UIButton *)sender
{
    if (_photoSelectedState) {
        self.returnSelectedImage(_selectedImageView.image);
        if (_tabbarState==1) {
            self.navigationController.navigationBar.hidden=YES;
            self.tabBarController.tabBar.hidden=YES;
        }else{
            self.navigationController.navigationBar.hidden=YES;
            self.tabBarController.tabBar.hidden=NO;
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [self.view showTiShiMessage:@"请选择头像"];
    }
    
}
//返回上一页
-(void)leftBtnClick:(UIButton *)sender
{
    if (_tabbarState==1) {
        self.navigationController.navigationBar.hidden=YES;
        self.tabBarController.tabBar.hidden=YES;
    }else{
        self.navigationController.navigationBar.hidden=YES;
        self.tabBarController.tabBar.hidden=NO;
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI
{
    CGFloat btn_w=(self.view.frame.size.width-10*5)/4.0;
    CGFloat btn_h=btn_w;
    
    //进入相册选择图片
    UIImageView *selectedImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20+44+10, btn_w, btn_h)];
    selectedImageView.image=[UIImage imageNamed:@"btn_add_photo_s"];
    _selectedImageView=selectedImageView;
    [self.view addSubview:selectedImageView];
    
    //盖按钮
    UIButton *selectedImageViewBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    selectedImageViewBtn.frame=selectedImageView.frame;
    [selectedImageViewBtn addTarget:self action:@selector(selectedImageViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:selectedImageViewBtn];
    //分割线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(selectedImageView.frame)+10, self.view.frame.size.width, 1)];
    line.backgroundColor=[UIColor colorWithRed:212/255.0 green:213/255.0 blue:214/255.0 alpha:1];
    [self.view addSubview:line];
    
    //scrollView
    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(line.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(line.frame))];
    scrollView.showsVerticalScrollIndicator=NO;
   // scrollView.bounces=NO;
    NSInteger hang;
    if (_imageArray.count%4==0) {
        hang=_imageArray.count/4;
    }else{
        hang=_imageArray.count/4+1;
    }
    
    for (int i=0; i<hang; i++) {
        for (int j=0; j<4; j++) {
            if (i*4+j<_imageArray.count) {
                UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10+(btn_w+10)*j, 10+(btn_h+10)*i, btn_w, btn_h)];
                imgView.image=[UIImage imageNamed:_imageArray[i*4+j]];
                [scrollView addSubview:imgView];
                imgView.userInteractionEnabled=YES;
                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHappen:)];
                [imgView addGestureRecognizer:tap];
                
                
//                UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
//                btn.frame=CGRectMake(10+(btn_w+10)*j, 10+(btn_h+10)*i, btn_w, btn_h);
//                [btn setBackgroundImage:[UIImage imageNamed:_imageArray[i*4+j]] forState:UIControlStateNormal];
//                [scrollView addSubview:btn];
            }

        }
       
    }
    scrollView.contentSize=CGSizeMake(self.view.frame.size.width, (10+btn_h)*hang+20);
    [self.view addSubview:scrollView];
    
    
}
#pragma mark 本地图片选择
-(void)tapHappen:(UITapGestureRecognizer *)tap
{
    UIImageView *imgView=(id)tap.view;
    //_selectedImageView.image=imgView.image;
    self.returnSelectedImage(imgView.image);
    if (_tabbarState==1) {
        self.navigationController.navigationBar.hidden=YES;
        self.tabBarController.tabBar.hidden=YES;
    }else{
        self.navigationController.navigationBar.hidden=YES;
        self.tabBarController.tabBar.hidden=NO;
    }
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark 进入相册进行图片选择
-(void)selectedImageViewBtnClick:(UIButton *)sender
{
    UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"拍照", nil];
    [sheet showInView:self.view];

}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"================%ld",buttonIndex);
    if (buttonIndex==0) {
        UIImagePickerController *picker=[[UIImagePickerController alloc]init];
        picker.delegate=self;
        picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];
    }else if(buttonIndex==1){
        UIImagePickerController *picker=[[UIImagePickerController alloc]init];
        picker.delegate=self;
        picker.sourceType=UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    }else{
        
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //NSLog(@"===========%@",info);
    _selectedImageView.image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    _photoSelectedState=1;
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
